package com.show.dao02;

import com.show.entity.User;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface IUserDao {

    // 通过ID查找用户
    @Select("select * from user where id = #{id}")
    User findUserById(Integer id);

    // 查询所有用户
    @Select("select * from user")
    @Results({
            // id: true 说明为主键
            // 配置用户表对应的列的关系
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "birthday", column = "birthday"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "address", column = "address"),
            // 配置账户表的关系
            // property 对象的属性名,对应User对象中的List<Account> accounts 属性
            // column 对应的主键 id (若是account查询user 则是外键uid)
            // many 提供一个注解@Many 指定查询方法的全路径  fetchType 指定加载方式 , Lazy为延迟加载
            @Result(property = "accounts", column = "id", javaType = List.class, many = @Many(select = "com.show.dao02.IAccountDao.findAccountById",fetchType = FetchType.LAZY))

    })
    List<User> findAll();
}
